package com.summerbird.mallchat.common.chat.consumer;

import com.summerbird.mallchat.common.chat.dao.ContactDao;
import com.summerbird.mallchat.common.chat.dao.MessageDao;
import com.summerbird.mallchat.common.chat.dao.RoomDao;
import com.summerbird.mallchat.common.chat.dao.RoomFriendDao;
import com.summerbird.mallchat.common.chat.domain.entity.GroupMember;
import com.summerbird.mallchat.common.chat.domain.entity.Message;
import com.summerbird.mallchat.common.chat.domain.entity.Room;
import com.summerbird.mallchat.common.chat.domain.entity.RoomFriend;
import com.summerbird.mallchat.common.chat.domain.enums.RoomTypeEnum;
import com.summerbird.mallchat.common.chat.domain.vo.response.ChatMessageResp;
import com.summerbird.mallchat.common.chat.service.ChatService;
import com.summerbird.mallchat.common.chat.service.cache.GroupMemberCache;
import com.summerbird.mallchat.common.chat.service.cache.HotRoomCache;
import com.summerbird.mallchat.common.chat.service.cache.RoomCache;
import com.summerbird.mallchat.common.common.constant.MQConstant;
import com.summerbird.mallchat.common.common.domain.dto.MsgSendMessageDTO;
import com.summerbird.mallchat.common.user.dao.UserFriendDao;
import com.summerbird.mallchat.common.user.service.impl.PushService;
import com.summerbird.mallchat.common.websocket.service.adapter.WebSocketAdapter;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.*;

@RocketMQMessageListener(topic = MQConstant.SEND_MSG_TOPIC, consumerGroup = MQConstant.SEND_MSG_GROUP)
@Component
public class MsgSendConsumer implements RocketMQListener<MsgSendMessageDTO> {

    @Autowired
    private MessageDao messageDao;
    @Autowired
    private RoomDao roomDao;
    @Autowired
    private RoomCache roomCache;
    @Autowired
    private ChatService chatService;
    @Autowired
    private GroupMemberCache groupMemberCache;
    @Autowired
    private RoomFriendDao roomFriendDao;
    @Autowired
    private ContactDao contactDao;
    @Autowired
    private PushService pushService;
    @Autowired
    private HotRoomCache hotRoomCache;

    @Override
    public void onMessage(MsgSendMessageDTO dto) {
        // 获取消息信息以及房间信息
        Message message = messageDao.getById(dto.getMsgId());
        Room room = roomCache.get(message.getRoomId());
        ChatMessageResp msgResp = chatService.getMsgResp(message, null);
        // 所有房间更新最新消息
        roomDao.refreshActiveTime(room.getId(), message.getId(), message.getCreateTime());
        roomCache.delete(room.getId());
        if(room.isHotRoom()){ // 热点房间则推送给所有人
            // 更新热门群聊事件
            hotRoomCache.refreshActiveTime(room.getId(), message.getCreateTime());
            // 推送所有人
            pushService.sendPushMsg(WebSocketAdapter.buildMsgSend(msgResp));
        } else { // 非热点则推送房间内人的收信箱
            List<Long> memberUidList = new ArrayList<>();
            // 对普通群聊所有人推送
            if(Objects.equals(room.getType(), RoomTypeEnum.GROUP.getType())){
                memberUidList = groupMemberCache.getMemberUidList(room.getId());
            }else {// 对单聊推送
                RoomFriend roomFriend = roomFriendDao.getByRoomId(room.getId());
                memberUidList = Arrays.asList(roomFriend.getUid1(), roomFriend.getUid2());
            }
            //更新所有群成员的会话时间
            contactDao.refreshOrCreateActiveTime(room.getId(), memberUidList, message.getId(), message.getCreateTime());
            //推送房间成员
            pushService.sendPushMsg(WebSocketAdapter.buildMsgSend(msgResp), memberUidList);
        }
    }
}
